﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Sunboxsoft.WinForm.Shell
{
    public partial class LaunchModuleForm : Form
    {
        private Thread _thread = null;

        private IModule _module;

        public LaunchModuleForm(IModule module)
        {
            InitializeComponent();

            _module = module;
        }

        protected override void OnLoad(EventArgs e)
        {
            _thread = new Thread(Running);
            _thread.Start();

            base.OnLoad(e);
        
        }

        private void Running()
        {
            bool result = _module.Load(UIContext.Current.ModuleRuntimeContext, new ModuleLoadingProgress((progress, message)=>{
                this.BeginInvoke(new Action(() =>
                {
                    ctlProgressBarControl.EditValue = (int)(ctlProgressBarControl.Properties.Maximum * progress);
                    ctlLabelText.Text = message;
                }));
            }));
          
            this.BeginInvoke(new Action<bool>(OnLaunchCompleted), result);
        }

        private void OnLaunchCompleted(bool isSuccess)
        {
            if (isSuccess)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }
    }
}
